﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace FileDistribution
{
    public partial class MainForm : Form
    {
        List<DataView> historys = new List<DataView>();
        List<string> tags = new List<string>();
        public MainForm()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            try
            {
                var path = Application.StartupPath + "/system/data.dat";
                BinaryFormatter bf = new BinaryFormatter();
                var data= File.ReadAllBytes(path);
                MemoryStream ms=new MemoryStream(data);
                historys = bf.Deserialize(ms) as List<DataView>;
                cbbhistroy.Items.Clear();
                foreach (var item in historys)
                {
                    cbbhistroy.Items.Add(item.Name);
                }
                cbbhistroy.Refresh();
            }
            catch (Exception)
            {
                
            }
        }

        private void btnloadresoure_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog()==DialogResult.OK)
            {
                txtrpath.Text = fbd.SelectedPath;
            }
        }

        private void btnloadtag_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txttagpath.Text = fbd.SelectedPath;
            }
        }

        private void btnaddtag_Click(object sender, EventArgs e)
        {
            var tagpath = txttagpath.Text;
            if (string.IsNullOrEmpty(tagpath))
            {
                MessageBox.Show("选择分发对象目录！");
            }
            else
            {
                var temp = tags.Where(w => w == tagpath).FirstOrDefault();
                if (temp!=null)
                {
                    MessageBox.Show("分发对象已经存在！");
                }
                else
                {
                    tags.Add(tagpath);
                    lbxtags.Items.Add(tagpath);
                }
            }
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            var rootpath = Application.StartupPath + "/system/";
            if (!Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            
            if (string.IsNullOrEmpty(txtrpath.Text))
            {
                MessageBox.Show("请输入分发源！");
                return;
            }
            DataView dv = new DataView();
            dv.Name =txtrpath.Text;
            dv.TagPaths = tags;
            dv.ResourcePath = txtrpath.Text;
            var hsdv = historys.Where(w => w.Name == dv.Name).FirstOrDefault();
            if (hsdv!=null)
            {
                historys.RemoveAll(w => w.Name == dv.Name);
            }
            historys.Add(dv);
            if (tags!=null&&tags.Count>0)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(ms, historys);
                    File.WriteAllBytes(rootpath + "data.dat", ms.ToArray());
                }
                LoadingMsg lm = new LoadingMsg(tags,txtrpath.Text,this);
                lm.ShowDialog();
            }
            else
            {
                MessageBox.Show("请录入分发对象！");
            }
            Init();
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lbxtags_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int posindex = lbxtags.IndexFromPoint(new Point(e.X, e.Y));
                lbxtags.ContextMenuStrip = null;
                if (posindex >= 0 && posindex < lbxtags.Items.Count)
                {
                    lbxtags.SelectedIndex = posindex;
                    cmslbspaths.Show(lbxtags, new Point(e.X, e.Y));
                }
            }
            lbxtags.Refresh();
        }

        private void tsmdel_Click(object sender, EventArgs e)
        {
            tags.Remove(lbxtags.SelectedItem.ToString());
            lbxtags.Items.RemoveAt(lbxtags.SelectedIndex);
            lbxtags.Refresh();
        }

        private void cbbhistroy_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbxtags.Items.Clear();
            tags.Clear();
            var name = cbbhistroy.Text;
            var dv = historys.Where(w => w.Name == name).FirstOrDefault();
            if (dv!=null)
            {
                foreach (var item in dv.TagPaths)
                {
                    tags.Add(item);
                    lbxtags.Items.Add(item);
                }
                txtrpath.Text = dv.ResourcePath;
            }
        }
    }
}
